package com.easyadmin.pro.tool;

import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.modules.sys.vo.DictItemVo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;

/**
 * packageName com.easyadmin.pro.tool
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className DictGeneratorUtils
 * @date 2024/8/20
 * @description 字典生成器
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictGeneratorUtils {

    private final DataSource dataSource;

    private String GENERATOR_DICT_SQL = "CREATE TABLE %s (\n" +
            "           `id` bigint(32) NOT NULL COMMENT '主键',\n" +
            "           PRIMARY KEY (`id`) USING BTREE\n" +
            "        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='%s';";

    private String GENERATOR_DICT_ITEM_SQL = "ALTER TABLE %s ADD COLUMN %s %s ";

    private String DROP_DICT_SQL = "DROP TABLE IF EXISTS %s";

    private String DROP_DICT_ITEM_SQL = "ALTER TABLE %s DROP COLUMN %s";

    /**
     * 生成字典
     * @param dictCode
     * @param dictName
     */
    public void generatorDict(String dictCode, String dictName) {
        String executeSql = String.format(GENERATOR_DICT_SQL, dictCode, dictName);
        execute(executeSql);
    }

    /**
     * 执行
     * @param executeSql
     * @throws SQLException
     */
    @SneakyThrows
    private void execute(String executeSql)  {
        Connection connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        statement.execute(executeSql);
    }

    /**
     * 生成字典属性
     * @param dictItemVo
     */
    public void generatorDictItem(DictItemVo dictItemVo) {
        String executeSql = String.format(GENERATOR_DICT_ITEM_SQL, dictItemVo.getDictCode(),
                dictItemVo.getDictItemCode(), dictItemVo.getDictItemType());
        if (Objects.nonNull(dictItemVo.getDictItemSize()) && dictItemVo.getDictItemSize() > 0) {
            executeSql = executeSql.concat("(").concat(dictItemVo.getDictItemSize().toString()).concat(")");
        }

        if (Objects.equals(StatusConstants.STATUS_1, dictItemVo.getRequired())) {
            executeSql = executeSql.concat(" not null ");
        } else {
            executeSql = executeSql.concat(" null ");
        }
        executeSql = executeSql.concat("comment ").concat("'").concat(dictItemVo.getDictItemName()).concat("'");
        execute(executeSql);
    }

    /**
     * 删除属性
     * @param dictItemCode
     * @param dictCode
     */
    public void removeDictItem(String dictItemCode, String dictCode) {
        String executeSql = String.format(DROP_DICT_ITEM_SQL, dictCode, dictItemCode);
        execute(executeSql);
    }

    /**
     * 删除字典
     * @param dictCode
     */
    public void deleteDict(String dictCode) {
        String executeSql = String.format(DROP_DICT_SQL, dictCode);
        execute(executeSql);
    }
}